 <?php
require_once ('utils' . DIRECTORY_SEPARATOR . 'HHAbstract.php');

set_time_limit ( - 1 );

class HHSData extends HHAbstract {
	
	function __construct() {
		parent::__construct ();
	}
	
	function __destruct() {
		parent::__destruct ();
	}
	
	function constructClass() {
		// Get some data from the database
		//$query = "Select net_to_hud, date_accepted from bid_results WHERE state='MD' order by date_accepted asc limit 10000";
		$query = 'Select 
		COALESCE(CAST(format((br.net_to_hud/pl.price)*100,2) AS DECIMAL(5,2)),0) as `percent_net_to_hud`,
		COALESCE(CAST(format((br.net_to_hud/pl.as_is_value )*100,2) AS DECIMAL(5,2)),0) as `percent_as_is`,
		ABS(COALESCE(datediff(pl.list_date,br.date_accepted),0)) as `DOM`,
		br.state
		from bid_results as br
		inner join property_list as pl on pl.case = br.case
		WHERE
		pl.state in ("MD","DC","VA")
		AND br.purchaser_type = "INV"
		HAVING `DOM` > 0 AND `DOM`<= 90
		order by `DOM` ASC';
		$result = $this->runQuery ( $query, $this->getDBConnection ()->getDBConnection () );
		$json = array ();
		$stateMap = array();
		while ( ($row = $result->fetch_assoc ()) ) {
			$percentNetToHud = floatval ( $row ['percent_net_to_hud'] );
			$percentAsIs = floatval ( $row ['percent_as_is'] );
			$state = $row ['state'];
			$DOM = $row ['DOM'];
			if(!isset($stateMap[$state])){
				$stateMap[$state] = array(
				array ('label' => $state . ' % net to hud ', 'data' => array () ),
				array ('label' => $state . ' % As Is ', 'data' => array ())
				);
				 
			}
			$stateMap[$state][0]['data'][] = array ($DOM, $percentNetToHud );
			$stateMap[$state][1]['data'][] = array ($DOM, $percentAsIs );
		}
		foreach ($stateMap as $stateArray){
			$json[] = $stateArray[0];
			$json[] = $stateArray[1];
		}
		echo (json_encode ( $json ));
		die ();
	}
}

$obj = new HHSData ( );
?>